package com.example.springboot001.controller;

import com.alibaba.fastjson.JSON;
import com.example.springboot001.model.DemoUser;
import com.example.springboot001.model.RelTelphoneMac;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @program: springboot001
 * @description: 测试
 * @author: XianJin.Lu
 * @create: 2021-09-25 16:41
 **/
@Controller
public class Ceshi {


    @RequestMapping("/one")
    public  String method(){
        return "hello";
    }

    @RequestMapping("/two")
    public  String method2(){
        return "hello2";
    }


    @RequestMapping("/hello.html")
    public String hello(Model model){

        model.addAttribute("msg","后端传的数据");
        return "hello";
    }


    @RequestMapping(value="/addUser",method= RequestMethod.POST)
    @ResponseBody
    public String addUser7(@RequestBody DemoUser user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "success";
    }


    @RequestMapping(value="/addUser2",method= RequestMethod.POST)
    @ResponseBody
    public String addUser2(@RequestBody RelTelphoneMac user) {
        System.out.println(user);
        return "success";
    }



    /*如果设置了      async: false,
  dataType: 'json',   后端返回不许是JSON  才能回到 success中
   ！*/
    @RequestMapping(value="/addUser3",method= RequestMethod.POST)
    @ResponseBody
    public String addUser3( @RequestParam HashMap<String, Object> map,Model model) {
        System.out.println(map);
      model.addAttribute("lds","1111");

      String s="{\"abc\",\"111\"}";
        String s1 = JSON.toJSONString(s);     //返回的是json 和  dataType: 'json'对应    async: false,同步执行回到success
        return s1;

    }



    @RequestMapping(value="/addUser4",method= RequestMethod.POST)
    @ResponseBody
    public String addUser4( @RequestParam HashMap<String, Object> map,Model model) {
        System.out.println(map);

      //返回的是text 和  dataType: 'text'对应    async: false,同步执行回到success
        return "2222";
    }
}
